package com.example.demo.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/satoken")
@Slf4j
public class SaTokenController {

    // login
    @GetMapping("/login")
    public Result<String> login(@RequestParam String username, @RequestParam String password) {
        log.info("用户信息:{}, {}", username, password);
        if (username.equals("admin") && password.equals("123456")) {
            StpUtil.login(10001);
            return Result.success(StpUtil.getTokenValue());
        }
        return Result.error("登录识别失败");
    }

    // 是否登录 返回token
    @GetMapping("/info")
    public Result<String> info(@RequestHeader("Auth") String auth) {
        log.info("<UNK>:{}", auth);
        log.info("是否登录: {}", StpUtil.isLogin());
        return Result.success(StpUtil.getTokenValue());
    }

    // 退出登录
    @GetMapping("/logout")
    public Result<Boolean> logout() {
        StpUtil.logout();
        return Result.success(true);
    }

    // 检验当前会话是否已经登录, 如果未登录，则抛出异常：`NotLoginException`
    @GetMapping("/checkLogin")
    public Result<Boolean> checkLogin() throws NotLoginException {
        StpUtil.checkLogin();
        return Result.success(true);
    }

    // 读取账户权限集合

}
